﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.StoreHouse;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.StoreHouse
{
    public class WmWarehouseLocationServices : IWmWarehouseLocationServices
    {
        public WmWarehouseLocationServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(WmWarehouseLocation model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(WmWarehouseLocation model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
        //查询所有
        public async Task<IEnumerable<WmWarehouseLocation>> GetAllAsync()
        {
            string sql = "select * from WmWarehouseLocation";
            var list = await Db.QueryAllAsync<WmWarehouseLocation>(sql);
            return list;
        }
        //分页
        public async Task<IEnumerable<WmWarehouseLocation>> GetAsync(int PageIndex, int PageSize, string LocationNumber, string LocationName)
        {
            string sql = "select * from WmWarehouseLocation where 1=1 ";
            if (!string.IsNullOrEmpty(LocationNumber))
            {
                sql += " and LocationNumber like @LocationNumber ";
            }
            if (!string.IsNullOrEmpty(LocationName))
            {
                sql += " and LocationName like @LocationName ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<WmWarehouseLocation>(sql, new { PageIndex, PageSize, LocationNumber = $"%{LocationNumber}%", LocationName = $"%{LocationName}%" });
            return list;
        }
        //查询搜索
        public async Task<int> GetCountAsync(string LocationNumber, string LocationName)
        {
            string sql = "select count(*) from WmWarehouseLocation where 1=1 ";
            if (!string.IsNullOrEmpty(LocationNumber))
            {
                sql += "and LocationNumber like @LocationNumber ";
            }
            if (!string.IsNullOrEmpty(LocationName))
            {
                sql += "and LocationName like @LocationName ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { LocationNumber = $"%{LocationNumber}%", LocationName = $"%{LocationName}%" });
            return count;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete WmWarehouseLocation where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete WmWarehouseLocation where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
    }
}
